#!/bin/sh
#   The above line causes this script to be run using the Bourne Shell (sh)
#######################################################################
#
#   Script to maintain a contacts database.
#
#######################################################################

#
#   Define the name of the file
#
fname=names.dat

#
#   If the file does not exist, create it
#
[ ! -f $fname ] && > $fname

#
#   Loop forever - until they want to exit the program
#
while true
do
    #
    #   Display the menu
    #
    clear
    echo "\n\t\tSHELL PROGRAMMING DATABASE"
    echo "\t\t\tMAIN MENU"
    echo "\nWhat do you wish to do?\n"
    echo "\t1.  Create records"
    echo "\t2.  View records"
    echo "\t3.  Search for records"
    echo "\t4.  Delete records that match a pattern"
    echo

    #
    #   Prompt for an answer
    #
    echo "Answer (or 'q' to quit)? \c"
    read ans junk

    #
    #   Empty answers (pressing ENTER) cause the menu to redisplay,
    #   so .... back around the loop
    #   We only make it to the "continue" bit if the "test"
    #   program ("[") returned 0 (True)
    #
    [ "$ans" = "" ] && continue

    #
    #   Decide what to do
    #
    case $ans in
        1)
            #
            #   Read in the contact details from the keyboard
            #
            echo "Please enter the following contact details:"
            echo
            echo "Given name: \c"
            read name
            echo "   Surname: \c"
            read surname
            echo "   Address: \c"
            read address
            echo "      City: \c"
            read city
            echo "     State: \c"
            read state
            echo "       Zip: \c"
            read zip

            #
            #   Write the details to the text file
            #
            echo $name:$surname:$address:$city:$state:$zip >> $fname
            ;;
        2)
            #
            #   Show what's currently in the file
            #
            (
                echo
                echo Here are the current contacts in the database:
                echo
                echo "First Name    Surname         Address             City           State Zip"
                echo "============================================================================"

                #
                #   Display the line correctly formatted.
                #   Use awk for the formatting.
                #   The "-F :" bit causes awk to perceive fields as being
                #   separated by colons.
                #   "%-14.14s" means display a string in a field width
                #   of 14, left-justified.
                #
                sort -t : +1 $fname | awk -F : '{printf("%-14.14s%-16.16s%-20.20s%-15.15s%-6.6s%-5.5s\n", $1, $2, $3, $4, $5, $6)}'
            ) | more
    
            #
            #  And display how many there are
            #
            echo
            echo There are `cat $fname | wc -l` contacts in the database
            ;;
        3)
            echo The Search case is not implemented yet
            ;;
        4)
            echo The Delete case is not implemented yet
            ;;
        q*|Q*)
            exit 0
            ;;
        *)
            echo "please enter a number between 1 and 4"
            ;;
    esac

    #
    #   Pause to give the user a chance to see what's on the screen
    #
    echo "Hit <ENTER> to continue: \c"
    read junk
done
